package io.geekidea.springbootplus.system.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.geekidea.springbootplus.framework.common.service.impl.BaseServiceImpl;
import io.geekidea.springbootplus.framework.core.pagination.PageInfo;
import io.geekidea.springbootplus.framework.core.pagination.Paging;
import io.geekidea.springbootplus.system.entity.SysCategory;
import io.geekidea.springbootplus.system.entity.SysProduct;
import io.geekidea.springbootplus.system.mapper.SysProductMapper;
import io.geekidea.springbootplus.system.param.SysProductPageParam;
import io.geekidea.springbootplus.system.service.SysProductService;
import io.geekidea.springbootplus.system.vo.SysProductQueryVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class SysProductServiceImpl extends BaseServiceImpl<SysProductMapper, SysProduct> implements SysProductService {

    @Resource
    private SysProductMapper sysProductMapper;

    @Override
    public boolean saveSysProduct(SysProduct sysProduct) throws Exception {
        return sysProductMapper.insert(sysProduct) > 0;
    }

    @Override
    public boolean updateSysProduct(SysProduct sysProduct) throws Exception {
        return sysProductMapper.updateById(sysProduct) > 0;
    }

    @Override
    public boolean deleteSysProduct(Integer id) throws Exception {
        return sysProductMapper.deleteById(id) > 0;
    }


    @Override
    public Paging<SysProductQueryVo> getSysProductPageListByParam(SysProductPageParam sysProductPageParam) throws Exception {
        Page<SysProductQueryVo> page = new PageInfo<>(sysProductPageParam, OrderItem.desc(getLambdaColumn(SysProduct::getId)));
        IPage<SysProductQueryVo> iPage = sysProductMapper.getSysProductPageListByParam(page, sysProductPageParam);
        return new Paging(iPage);
    }

    @Override
    public SysProductQueryVo findSysProduct(Integer id) {
        return sysProductMapper.findProductById(id);
    }


    @Override
    public boolean updateBatchSysProductUpStatus(List<Integer> ids) {
        return sysProductMapper.updateAllSysProductUpStatus(ids);
    }

    @Override
    public boolean updateBatchSysProductDownStatus(List<Integer> ids) {
        return sysProductMapper.updateAllSysProductDownStatus(ids);
    }

    @Override
    public boolean deleteBatchSysProduct(List<Integer> ids) {
        return sysProductMapper.batchDeleteSysProduct(ids);
    }

    @Override
    public List<SysProductQueryVo> findSysProductBySales() {
        return sysProductMapper.getSysProductBySales();
    }

    @Override
    public List<SysProductQueryVo> getSysProductByTags(String tags) throws Exception {
        return sysProductMapper.findSysProductByTags(tags);
    }
}
